import 'package:flutter/material.dart';
import 'package:urltest/web_page.dart';
import 'dart:js' as js;
import 'package:http/http.dart' as http;

import 'config_utils.dart';
import 'get_mac_page.dart';

void main() async {
  await ConfigUtils.loadServerConfig();
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: ElevatedButton(
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (content) => const GetMacPage(),
                    ),
                  );
                },
                child: const Text('跳转到获取mac页面'),
              ),
            ),
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: ElevatedButton(
                onPressed: () {
                  //调用js的函数来执行想要的操作
                  sendToPlugin('open_new_tab');
                },
                child: const Text('http.post 调用Chrome插件'),
              ),
            ),
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: ElevatedButton(
                onPressed: () {
                  //调用js的函数来执行想要的操作
                  js.context.callMethod("getMac");
                },
                child: const Text('js.callMethod 调用Chrome插件'),
              ),
            ),
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: ElevatedButton(
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (content) =>
                          WebViewPage(url: ConfigUtils.config.url),
                    ),
                  );
                },
                child: const Text('嵌入页面预览'),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

Future<void> sendToPlugin(String message) async {
  final response = await http.post(
      Uri.parse('chrome-extension://your_extension_id/'),
      body: {'message': message});
  print('收到来自Chrome插件的响应： ${response.body}');
}
